<?php
/**
 * Minhlog Minh_Resource 路由资源类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Minh
 * @package    Minh_Resource
 * @copyright  Copyright (c) 2009-2010 Minh.Shu Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Router.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Minh_Resource_Router extends Zend_Application_Resource_ResourceAbstract
{
    protected $_router;

    public function init ()
    {
        return $this->getRouter();
    }

    /**
     * 获取视图资源对象
     * @return $view
     */
    public function getRouter ()
    {
        if (null === $this->_router) {
            $router = Zend_Controller_Front::getInstance()->getRouter();
            $options = $this->getOptions();

            //添加静态路由
            foreach($options['static'] as $key => $static){
                $route = new Zend_Controller_Router_Route_Static($static['routerule'],
                        array('controller' => $static['controller'],
                                'action' => $static['action'] ,
                                'module' => $static['module']));
                $router->addRoute($key, $route);
            }

            //添加正则路由
            foreach($options['regex'] as $key => $regex){
                $route = new Zend_Controller_Router_Route_Regex(
                        $regex['routerule'], $regex['urlrule'], $regex['paramrule'], $regex['regexrule']);
                $router->addRoute($key, $route);
            }

            $this->_router = $router;
        }
        return $this->_router;
    }
}